#include "dialog.h"

#include <QString>
#include <QPalette>
#include <QDir>
#include <QDebug>
#include <QtGui>


Dialog::Dialog(QWidget *parent) :
    QDialog(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);//隐藏边框和标题栏
    init();
}

void Dialog::init()
{
    QPixmap pixmap(QDir::toNativeSeparators(":/image/res/ground.png"));
    QPalette pal =this->palette();
    pal.setBrush(QPalette::Background, QBrush(pixmap));
    setPalette(pal);

    btn0 = new QPushButton("0");
    btn0->setFixedSize(50,50);
    btn0->installEventFilter(this);
    btn0->setStyleSheet("QPushButton{border:none;}");
    btn0->setFocusPolicy(Qt::NoFocus);

    btn1 = new QPushButton("1");
    btn1->setFixedSize(50,50);
    btn1->installEventFilter(this);
    btn1->setStyleSheet("QPushButton{border:none;}");
    btn1->setFocusPolicy(Qt::NoFocus);

    btn2 = new QPushButton("2");
    btn2->setFixedSize(50,50);
    btn2->installEventFilter(this);
    btn2->setStyleSheet("QPushButton{border:none;}");
    btn2->setFocusPolicy(Qt::NoFocus);

    btn3 = new QPushButton("3");
    btn3->setFixedSize(50,50);
    btn3->installEventFilter(this);
    btn3->setStyleSheet("QPushButton{border:none;}");
    btn3->setFocusPolicy(Qt::NoFocus);

    btn4 = new QPushButton("4");
    btn4->setFixedSize(50,50);
    btn4->installEventFilter(this);
    btn4->setStyleSheet("QPushButton{border:none;}");
    btn4->setFocusPolicy(Qt::NoFocus);

    btn5 = new QPushButton("5");
    btn5->setFixedSize(50,50);
    btn5->installEventFilter(this);
    btn5->setStyleSheet("QPushButton{border:none;}");
    btn5->setFocusPolicy(Qt::NoFocus);

    btn6 = new QPushButton("6");
    btn6->setFixedSize(50,50);
    btn6->installEventFilter(this);
    btn6->setStyleSheet("QPushButton{border:none;}");
    btn6->setFocusPolicy(Qt::NoFocus);

    btn7 = new QPushButton("7");
    btn7->setFixedSize(50,50);
    btn7->installEventFilter(this);
    btn7->setStyleSheet("QPushButton{border:none;}");
    btn7->setFocusPolicy(Qt::NoFocus);

    btn8 = new QPushButton("8");
    btn8->setFixedSize(50,50);
    btn8->installEventFilter(this);
    btn8->setStyleSheet("QPushButton{border:none;}");
    btn8->setFocusPolicy(Qt::NoFocus);

    btn9 = new QPushButton("9");
    btn9->setFixedSize(50,50);
    btn9->installEventFilter(this);
    btn9->setStyleSheet("QPushButton{border:none;}");
    btn9->setFocusPolicy(Qt::NoFocus);

    btnC = new QPushButton("C");
    btnC->setFixedSize(50,50);
    btnC->installEventFilter(this);
    btnC->setStyleSheet("QPushButton{border:none;}");
    btnC->setFocusPolicy(Qt::NoFocus);

    btnL = new QPushButton("L");
    btnL->setFixedSize(50,50);
    btnL->installEventFilter(this);
    btnL->setStyleSheet("QPushButton{border:none;}");
    btnL->setFocusPolicy(Qt::NoFocus);

    btnHyphen = new QPushButton("-");
    btnHyphen->setFixedSize(50,50);
    btnHyphen->setFocusPolicy(Qt::NoFocus);
    btnHyphen->setStyleSheet("QPushButton{border:none;}");
    btnHyphen->installEventFilter(this);

    btnPoint = new QPushButton(".");
    btnPoint->setFixedSize(50,50);
    btnPoint->installEventFilter(this);
    btnPoint->setFocusPolicy(Qt::NoFocus);
    btnPoint->setStyleSheet("QPushButton{border:none;}");

    btnClear = new QPushButton("删除");
    btnClear->setFixedSize(50,50);
    btnClear->setFocusPolicy(Qt::NoFocus);
    btnClear->setStyleSheet("QPushButton{border:none;}");
    btnClear->installEventFilter(this);

    btnEnter = new QPushButton("确定");
    btnEnter->setFixedSize(50,50);
    btnEnter->installEventFilter(this);
    btnEnter->setFocusPolicy(Qt::NoFocus);
    btnEnter->setStyleSheet("QPushButton{border:none;}");

    QHBoxLayout *layoutLine1 = new QHBoxLayout;
    layoutLine1->addWidget(btn1);
    layoutLine1->addWidget(btn2);
    layoutLine1->addWidget(btn3);

    QHBoxLayout *layoutLine2 = new QHBoxLayout;
    layoutLine2->addWidget(btn4);
    layoutLine2->addWidget(btn5);
    layoutLine2->addWidget(btn6);

    QHBoxLayout *layoutLine3 = new QHBoxLayout;
    layoutLine3->addWidget(btn7);
    layoutLine3->addWidget(btn8);
    layoutLine3->addWidget(btn9);

    QHBoxLayout *layoutLine4 = new QHBoxLayout;
    layoutLine4->addWidget(btnPoint);
    layoutLine4->addWidget(btn0);
    layoutLine4->addWidget(btnHyphen);

    QVBoxLayout *layoutFunction = new QVBoxLayout;
    layoutFunction->addWidget(btnC);
    layoutFunction->addWidget(btnL);
    layoutFunction->addWidget(btnClear);
    layoutFunction->addWidget(btnEnter);


    QWidget *widgetFunction = new QWidget;
    widgetFunction->setStyleSheet("background-color:dark;");
    widgetFunction->setLayout(layoutFunction);

    QVBoxLayout *layoutLeft = new QVBoxLayout;
    layoutLeft->addLayout(layoutLine1);
    layoutLeft->addLayout(layoutLine2);
    layoutLeft->addLayout(layoutLine3);
    layoutLeft->addLayout(layoutLine4);

    QHBoxLayout *mainlayout = new QHBoxLayout;
    mainlayout->setContentsMargins(0,0,0,0);
    mainlayout->addLayout(layoutLeft);
    mainlayout->addWidget(widgetFunction);

    setFixedSize(220,250);

    setStyleSheet("background-color:yellow;border-radius:5px;font-size:20px;");

    setLayout(mainlayout);

    hide();
}

bool Dialog::eventFilter(QObject *watched,QEvent *e)
{
    QPushButton *listBtn[14] = {btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,btnPoint,btnHyphen,btnC,btnL};

    for(int i = 0; i < 14; i ++)
    {
        if(watched == listBtn[i])
        {
            if(e->type() == QEvent::MouseButtonPress)
            {
                listBtn[i]->setStyleSheet("background-color:blue;color:yellow;border:none;");
                //listBtn[i]->setFlat(true);
            }
            if(e->type() == QEvent::MouseButtonRelease)
            {
                listBtn[i]->setStyleSheet("QPushButton{border:none;}");
                //listBtn[i]->setFlat(false);

                strBuffer += listBtn[i]->text();

                setStr();
            }
        }
    }

   if(watched == btnEnter)
   {
       if(e->type() == QEvent::MouseButtonPress)
       {
          btnEnter->setFlat(false);
          btnEnter->setStyleSheet("background-color:blue;color:yellow;border:none;");
       }
       if(e->type() == QEvent::MouseButtonRelease)
       {
          btnEnter->setFlat(true);

          btnEnter->setStyleSheet("border:none;");

          activeEdit->clearFocus();

          activeEdit = NULL;

          clear();

          hide();
       }
   }

   if(watched == btnClear)
   {
       if(e->type() == QEvent::MouseButtonPress)
       {
          btnClear->setFlat(false);
          btnClear->setStyleSheet("background-color: blue;color: yellow;border:none;");
       }
       if(e->type() == QEvent::MouseButtonRelease)
       {
          btnClear->setFlat(true);
          btnClear->setStyleSheet("border:none;");

          if(strBuffer.length() > 0)
          {
                strBuffer.resize(strBuffer.length()-1);

                setStr();
          }
       }
   }

   return QWidget::eventFilter(watched, e);
}

Dialog::~Dialog()
{
    //delete ui;
}

void Dialog::setStr()
{
    if(activeEdit != NULL)
    {
        activeEdit->setText(strBuffer);
    }
}

void Dialog::setCurrentActive(QLineEdit* &editWriteto)
{
    strBuffer.resize(0);

    setVisible(true);

    activeEdit = editWriteto;

    setStr();
}

void Dialog::clear()
{
    strBuffer.resize(0);
}
